using System;
using System.Data;
using System.Linq;
using System.ComponentModel;
using System.Reflection;
using System.Collections.Generic;
using VSSAHT.Transfer;
using VSS_AHT.DataAccess;
using System.Xml.Linq;

namespace VSSAHT.DataAccess
{
    public partial class RoleMapper
    {
        #region ToSingle

        public static RoleDTO ToSingle(Role entity)
        {
            if (entity == null)
            {
                return null;
            }
            return new RoleDTO
            {
                ModifiedBy = entity.ModifiedBy,
                ModifiedDate = entity.ModifiedDate,
                Permits = entity.Permits.ToString(),
                Permitted = Serializer<PermissionDTO>.DeserializeObj(entity.Permits.ToString()),
                RoleCode = entity.RoleCode,
                RoleID = entity.RoleID,
                RoleName = entity.RoleName,
            };
        }

        #endregion

        #region ToEntity

        public static Role ToEntity(Role entity, RoleDTO businessobject)
        {
            if (businessobject == null)
            {
                return null;
            }
            entity.ModifiedBy = businessobject.ModifiedBy;
            entity.ModifiedDate = businessobject.ModifiedDate;
            entity.Permits = XElement.Parse(businessobject.Permits);
            entity.RoleCode = businessobject.RoleCode;
            entity.RoleID = businessobject.RoleID;
            entity.RoleName = businessobject.RoleName;
            entity.RowVersion = VersionConverter.ToBinary(businessobject.RowVersion);
            return entity;
        }

        #endregion

        #region ToBusinessObject

        public static RoleDTO ToBusinessObject(Role entity)
        {
            if (entity == null)
            {
                return null;
            }
            return new RoleDTO
            {
                ModifiedBy = entity.ModifiedBy,
                ModifiedDate = entity.ModifiedDate,
                Permits = entity.Permits.ToString(),
                Permitted = Serializer<PermissionDTO>.DeserializeObj(entity.Permits.ToString()),
                RoleCode = entity.RoleCode,
                RoleID = entity.RoleID,
                RoleName = entity.RoleName,
                RowVersion = VersionConverter.ToString(entity.RowVersion)
            };
        }

        #endregion

        #region ToBusinessObjectHasChildren

        public static RoleDTO ToBusinessObjectHasChildren(Role entity, params Type[] types)
        {
            if (entity == null)
            {
                return null;
            }
            RoleDTO businessobject = new RoleDTO
            {
                ModifiedBy = entity.ModifiedBy,
                ModifiedDate = entity.ModifiedDate,
                Permits = entity.Permits.ToString(),
                Permitted = Serializer<PermissionDTO>.DeserializeObj(entity.Permits.ToString()),
                RoleCode = entity.RoleCode,
                RoleID = entity.RoleID,
                RoleName = entity.RoleName,
                RowVersion = VersionConverter.ToString(entity.RowVersion)
            };
            if (types.Contains<Type>(typeof(User)) || types.Length == 0)
            {
                businessobject.UserCollection = new List<UserDTO>();
                foreach (var item in entity.Users)
                {
                    businessobject.UserCollection.Add(UserMapper.ToBusinessObject(item));
                }
            }
            return businessobject;
        }

        #endregion

    }
}
